/**
 * Created by Administrator on 2016/4/9.
 */
var is_login=false
function login(){
    var form=document.getElementById("login")
    var logins={
        accounts : form.accounts.value,
        password : form.password.value
    }
    send_user(logins)
}
function send_user(login_msg){
    $.ajax({
        type: "POST",
        url:"/app/user/login",
        data: login_msg,
        success: function(msg){
            json_user=eval('(' + msg + ')');
            if(json_user.res=="1"){
                alert("登陆失败")
            }else {
                var date=new Date();
                var expireDays=15;//将date设置为15天以后的时间
                date.setTime(date.getTime()+expireDays*24*3600*1000);//将userId和userName两个cookie设置为15天后过期
                window.localStorage.setItem("accounts",login_msg.accounts)
                window.localStorage.setItem("password",login_msg.password)
                /*document.cookie=("accounts="+escape(login_msg.accounts)+";expire="+date.toGMTString())
                 document.cookie=("password="+escape(login_msg.password)+";expire="+date.toGMTString())*/
                $("#login_ul button").hide("normal")
                if($.trim(json_user.data[0].have_img)=="0") json_user.data[0].accounts=0
                $("#login_ul").append(
                    '<li><img  src="/img/usrimg/'+$.trim(json_user.data[0].accounts)+'.jpg"  alt="'+json_user.data[0].name+'"style="padding-top:4px;width: 36px;height: 36px"></li>'+
                    '<li><a href="your/nice/url">'+json_user.data[0].name+'</a></li>'+
                    '<li><a href="/buy/bill.html">我的购买 <span class="label label-badge label-success" id="my_buy">2</span></a></li>'+
                    '<li><a href="/buy/bill.html">购物车 <span class="label label-badge label-success" id="shopping_cart">4</span></a></li>'+
                    '<li><a onclick="quit()">退出登陆</a></li>'
                )
                if(json_user.data[0].powers==1){
                    $("#login_ul").append(
                        '<li><a href="/user/admin.html">管理员</a></li>'
                    )
                }
                is_login=true
            }
        }
    });
}
function quit(){
    $("#login_ul li").hide("normal")
    $("#login_ul button").show("normal")
}
function check_user(){
    var accounts=window.localStorage.getItem("accounts");//读取数据
    var password=window.localStorage.getItem("password");
    if(accounts&&password){
        var logins={
            accounts : accounts,
            password : password
        }
        send_user(logins)
    }
}
$("#login input").on("change", function(){
    var chils=this.parentNode.parentNode.childNodes
    if(this.value.length>10||this.value.length<6){
        chils[2].innerHTML="长度不能小于6位，且不能大于10位"
        chils[0].className="col-md-2 control-label required"
        return
    }
    else {
        chils[2].innerHTML=""
        chils[0].className="col-md-2 control-label"
    }
})
